package com.fjnu.dao.impl;

import com.fjnu.dao.CommentDao;
import com.fjnu.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CommentDaoImpl implements CommentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private final CommentRowMapper commentRowMapper = new CommentRowMapper();

    @Override
    public int insert(Comment newComment) {
        if (newComment.getArticleId() == null || newComment.getUserId() == null) {
            return 0;
        }
        String sql = "INSERT INTO tb_comment (content, article_id, user_id, support, oppose, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, NOW(), NOW())";
        return jdbcTemplate.update(sql,
                newComment.getContent(),
                newComment.getArticleId(),
                newComment.getUserId(),
                newComment.getSupport(),
                newComment.getOppose());
    }

    @Override
    public int updateSupport(Integer commentId, Integer incremented) {
        String sql = "UPDATE tb_comment SET support = support + ?, update_time = NOW() WHERE id = ?";
        return jdbcTemplate.update(sql, incremented, commentId);
    }

    @Override
    public int updateOppose(Integer commentId, Integer incremented) {
        String sql = "UPDATE tb_comment SET oppose = oppose + ?, update_time = NOW() WHERE id = ?";
        return jdbcTemplate.update(sql, incremented, commentId);
    }

    @Override
    public List<Comment> selectAllCommentsForArticle(Integer articleId) {
        if (articleId == null) {
            return null;
        }
        String sql = "SELECT * FROM tb_comment WHERE article_id = ? ORDER BY create_time DESC";
        List<Comment> comments = jdbcTemplate.query(sql, commentRowMapper, articleId);
        return comments.isEmpty() ? null : comments;
    }

    @Override
    public List<Comment> selectCommentsWithLimitForArticle(Integer articleId, Integer startIndex, Integer endIndex) {
        if (articleId == null) {
            return null;
        }
        String sql = "SELECT * FROM tb_comment WHERE article_id = ? ORDER BY create_time DESC LIMIT ?, ?";
        List<Comment> comments = jdbcTemplate.query(sql, commentRowMapper,
                articleId, startIndex, endIndex - startIndex + 1);
        return comments.isEmpty() ? null : comments;
    }

    private static class CommentRowMapper implements RowMapper<Comment> {
        @Override
        public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
            Comment comment = new Comment();
            comment.setId(rs.getInt("id"));
            comment.setContent(rs.getString("content"));
            comment.setArticleId(rs.getInt("article_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setSupport(rs.getInt("support"));
            comment.setOppose(rs.getInt("oppose"));
            comment.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime());
            comment.setUpdateTime(rs.getTimestamp("update_time").toLocalDateTime());
            return comment;
        }
    }
}